package com.yunlan.materia.enums;

import com.google.common.collect.Lists;

import java.util.EnumSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author 云澜
 * @date 2023/4/17 3:18 下午
 */
public enum SortTypeEnum {
    asc("asc", "升序"),
    desc("desc", "降序");

    String code;
    String msg;

    SortTypeEnum(String code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    private static final Map<String, SortTypeEnum> VALUE_LOOKUP = new ConcurrentHashMap<>(values().length);

    static {
        for (SortTypeEnum type : EnumSet.allOf(SortTypeEnum.class)) {
            VALUE_LOOKUP.put(type.code, type);
        }
    }

    public static SortTypeEnum resolve(Integer value) {

        return (value != null ? VALUE_LOOKUP.get(value) : null);
    }

    public static SortTypeEnum fromValue(Integer value) {
        SortTypeEnum data = VALUE_LOOKUP.get(value);
        if (data == null) {
            throw new IllegalArgumentException("参数[" + value + "]不正确，没有找到对应的Enum");
        }
        return data;
    }

    /**
     * 将枚举转换成list格式，这样前台遍历的时候比较容易，列如 下拉框 后台调用toList方法，便可以得到code 和name
     *
     * @return
     */
    public static List<Map> enumList() {
        //javac通过自动推导尖括号里的数据类型.
        List list = Lists.newArrayList();
        for (SortTypeEnum airlineTypeEnum : SortTypeEnum.values()) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("code", airlineTypeEnum.getCode());
            map.put("msg", airlineTypeEnum.getMsg());
            list.add(map);
        }
        return list;
    }

    /**
     * 获取枚举的所有code
     *
     * @return
     */
    public static List enumCodeList() {
        List list = Lists.newArrayList();
        for (SortTypeEnum airlineTypeEnum : SortTypeEnum.values()) {
            list.add(airlineTypeEnum.getCode());
        }
        return list;
    }

    public String getCode() {
        return code;
    }

    public String getMsg(){
        return msg;
    }
}
